<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Activity;
use App\Models\EnrollPersonal;
use App\Models\User;
use App\Models\ActivityGather;
use Illuminate\Http\Request;
use Closure , Session , Cookie , Config;

class ActivityController extends AdminController
{

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model'] = 'activity';
        $this->_response['_active']['_action'] = '';
        $this->_response['_title'] = '小一农货-活动管理';
    }

    //活动首页
    public function index(Request $request)
    {
        $search = $request->all();
        $page = $request->get('page', 1);

        if(isset($search['type']) && $search['type'] == 'lishi'){
            $this->_response['_active']['_action'] = 'lishi';
        }else {
            $this->_response['_active']['_action'] = 'list';
        }

        $ActivityModel = new Activity();

        $sessionKey = Cookie::get(Config::get('session.admin_cookie'));
        $sessionData = Session::get($sessionKey);

        $name=$sessionData['userInfo']['name'];
        if($name == "管理员"){
            $data = $ActivityModel->getData($search, $page, 20);
            $this->_response['activity'] = $data;
        }else{
            $data = $ActivityModel->Login($search, $page, 20, $name);
            $this->_response['activity'] = $data;
        }

        $this->_response['activity']['pageData']['page'] = $page;
        $this->_response['activity']['pageData']['pageHtml'] = self::getPageHtml($page, $this->_response['activity']['pageData']['lastPage'], '/admin/activity?');

         return view('admin.activity.list', $this->_response);
    }
    //手机活动页面
    public function morindex()
    {
        $ActivityModel = new Activity();
        $this->_response['morindex'] = $ActivityModel->morindex();
       // return $this->_response;
        return view('admin.activity.morindex', $this->_response);
    }
    //手机报名页面
    public function morenroll($id)
    {
        $ActivityModel = new Activity();
        $this->_response['morenroll'] = $ActivityModel->morEnroll($id);
        $this->_response['id'] = $id;
        //return $this->_response['morenroll']['list'];
         return view('admin.activity.morenroll', $this->_response);
    }
    //手机历史报名页面
    public function morhistory($id)
    {
        $ActivityModel = new Activity();
        $this->_response['morenroll'] = $ActivityModel->morHisEnroll($id);
         $this->_response['id'] = $id;
        // return $this->_response;
        return view('admin.activity.morhistory', $this->_response);
    }
    //手机版报名删除页面
    public function morenrolldelete($enrollId,$personalId){
            $ActivityModel = new Activity();
            $Del = [
                'deleted_at' => date('Y-m-d H:i:s', time())
            ];
            if ($ActivityModel->personalupdate($enrollId,$personalId, $Del)) {
                return response()->json(['code' => 0, 'msg' => '删除成功']);
            } else {
                return response()->json(['code' => -1, 'msg' => '删除失败']);
            }
        }
     //手机版报名修改备注
        public function morenreason($enrollId,$personalId){
            $ActivityModel = new Activity();
            $reason= $_GET['reason'];
            $Edit = [
                'reason' => $reason,
            ];
            if ($ActivityModel->personalupdate($enrollId,$personalId,  $Edit)) {
                return response()->json(['code' => 0, 'msg' => '修改成功']);
            } else {
                return response()->json(['code' => -1, 'msg' => '修改失败']);
            }
        }
    //活动添加页面
    public function add()
    {
        $ActivityModel = new Activity();
        $this->_response['_active']['_action'] = 'add';
        $this->_response['class'] = $ActivityModel->class();
        $this->_response['user'] = $ActivityModel->user();
        // return $this->_response['cun'];
        return view('admin.activity.add', $this->_response);
    }
    public function doAdd(\App\Http\Requests\Activity $request)
    {
        $data = $request->all();

        $saveData = [
            'title' => $data['title'],
            'img' => $data['img'],
            'cost' => $data['cost'],
            'set_out' => $data['set_out'],
            'destination' => $data['destination'],
            'duration' => $data['duration'],
            'limit_of_number' => $data['limit_of_number'],
            'surplus_number' => $data['limit_of_number'],
            'route' => $data['route'],
            'type' => $data['type'],
            'class_id' => $data['class_id'],
            'user_id' => $data['user_id'],
            'strength' => $data['strength'],
            'view_index' => $data['view_index'],
            'collection_time' => $data['collection_time'],
            'collection_place' => $data['collection_place'],
            'sort' => $data['sort'],
            'on_line_time' => $data['on_line_time'],
            'off_line_time' => $data['off_line_time'],
            'created_at' => date('Y-m-d H:i:s', time())
        ];

        $ActivityModel = new Activity();
        if ($ActivityModel->saveData($saveData)) {
            return response()->json(['code' => 0, 'msg' => '添加成功',]);
        } else {
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }

    //活动合集
    public function gather()
    {
        $activity_gather = new ActivityGather();
        $res = $activity_gather->gather_first();
        $this->_response['_active']['_action'] = 'gather';
        $this->_response['active']= $res;
        return view('admin.activity.gather', $this->_response);
    }
    //添加活动合集
    public function gather_add(\App\Http\Requests\Editor $request)
    {

        $this->_response['_active']['_action'] = 'gather';
        $data = $request->all();
        $activity_gather = new ActivityGather();
        $res = $activity_gather->gather_add($data);
        if ($res){
            return response()->json(['code' => 0, 'msg' => '提交成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '提交失败']);
        }

    }
    //活动复制页面
    public function copy($id)
    {
        $ActivityModel = new Activity();
        $this->_response['_active']['_action'] = 'copy';
        $this->_response['activity'] = Activity::where('id', $id)->first();
        $this->_response['class'] = $ActivityModel->Class();
        $this->_response['user'] = $ActivityModel->User();
       // return  $this->_response['class'];

        return view('admin.activity.copy', $this->_response);
    }

    public function doCopy(\App\Http\Requests\Activity $request)
    {
        $data = $request->all();
        $saveData = [
            'title' => $data['title'],
            'img' => $data['img'],
            'cost' => $data['cost'],
            'set_out' => $data['set_out'],
            'destination' => $data['destination'],
            'duration' => $data['duration'],
            'limit_of_number' => $data['limit_of_number'],
            'surplus_number' => $data['limit_of_number'],
            'route' => $data['route'],
            'type' => $data['type'],
            'class_id' => $data['class_id'],
            'user_id' => $data['user_id'],
            'strength' => $data['strength'],
            'view_index' => $data['view_index'],
            'collection_time' => $data['collection_time'],
            'collection_place' => $data['collection_place'],
            'sort' => $data['sort'],
            'on_line_time' => $data['on_line_time'],
            'off_line_time' => $data['off_line_time'],
            'created_at' => date('Y-m-d H:i:s', time())
        ];

        $ActivityModel = new Activity();
        if ($ActivityModel->Copy($saveData)) {
            return response()->json(['code' => 0, 'msg' => '复制成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '复制失败']);
        }
    }

    //活动删除页面
    public function delete($id)
    {
        $Del = [
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        $ActivityModel = new Activity();
        if ($ActivityModel->Del($Del, $id)) {
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }

    //活动修改页面
    public function edit($id)
    {
        $ActivityModel = new Activity();
        $this->_response['_active']['_action'] = 'edit';
        $this->_response['activity'] = Activity::where('id', $id)->first();
        $this->_response['class'] = $ActivityModel->class();
        $this->_response['user'] = $ActivityModel->user();
        //return $this->_response;
        return view('admin.activity.edit', $this->_response);
    }

    public function doEdit(\App\Http\Requests\Activity $request)
    {
        $data = $request->all();

        $saveEdit = [
            'title' => $data['title'],
            'img' => $data['img'],
            'cost' => $data['cost'],
            'set_out' => $data['set_out'],
            'destination' => $data['destination'],
            'duration' => $data['duration'],
            'limit_of_number' => $data['limit_of_number'],
            'surplus_number' => $data['limit_of_number'],
            'route' => $data['route'],
            'type' => $data['type'],
            'class_id' => $data['class_id'],
            'user_id' => $data['user_id'],
            'strength' => $data['strength'],
            'view_index' => $data['view_index'],
            'collection_time' => $data['collection_time'],
            'collection_place' => $data['collection_place'],
            'sort' => $data['sort'],
            'on_line_time' => $data['on_line_time'],
            'off_line_time' => $data['off_line_time'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];

        $activityModel = new Activity();
        if ($activityModel->saveEdit($saveEdit, $data['id'])) {
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

    //活动费用
    public function cost($id)
    {
        $this->_response['_active']['_action'] = 'cost';
        $this->_response['activity'] = Activity::where('id', $id)->first();
        return view('admin.activity.cost', $this->_response);
    }

    public function costUpdate(\App\Http\Requests\Editor $request)
    {
        $data = $request->all();
        $saveEdit = [
            'id' => $data['id'],
            'cost_info' => $data['cost_info']
        ];
        $activityModel = new Activity();

        if ($activityModel->saveEdit($saveEdit, $data['id'])) {
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

    //路线说明
    public function route($id)
    {
        $this->_response['_active']['_action'] = 'cost';
        $this->_response['activity'] = Activity::where('id', $id)->first();
        return view('admin.activity.route', $this->_response);
    }

    public function routeUpdate(\App\Http\Requests\Editor $request)
    {
        $data = $request->all();
        // return response()->json(11);
        $saveEdit = [
            'route_info' => $data['route_info'],
        ];
        $activityModel = new Activity();
        if ($activityModel->saveEdit($saveEdit, $data['id'])) {
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

    //活动图片首页
    public function imgslist(Request $request, $id)
    {
        $search = $request->all();
        $page = $request->get('page', 1);
        $this->_response['_active']['_action'] = 'img';

        $imgModel = new Activity();
        $data = $imgModel->get($search, $page, 3,$id);

        $this->_response['imgs'] = $data;
        $this->_response['imgg'] = Activity::where('id', $id)->first();
        $this->_response['id'] = $id;
        // return $this->_response['imgs'];
        $this->_response['imgs']['pageData']['page'] = $page;
        $this->_response['imgs']['pageData']['pageHtml'] = self::getPageHtml($page, $this->_response['imgs']['pageData']['lastPage'], '/admin/activity/imgslist/'.$id.'?');
        return view('admin.activity.imgslist', $this->_response);
    }

    //活动图片添加页面
    public function imgsadd($id)
    {
        $ActivityModel = new Activity();
        $this->_response['_active']['_action'] = 'imgsadd';
        $this->_response['activity'] = Activity::where('id', $id)->first();
        $this->_response['id'] = $id;
        $this->_response['img'] = $ActivityModel->image();
        // return $this->_response['img'];
         return view('admin.activity.imgsadd', $this->_response);
    }

    public function imgsdoAdd(\App\Http\Requests\ActivityImg $request)
    {
        $data = $request->all();
        foreach ($data['img'] as $a) {
            $imgsadd = [
                'title' => $data['title'],
                'activity_id' => $data['activity_id'],
                'img' => $a['imgUrl'],
                'sort' => $data['sort'],
                'created_at' => date('Y-m-d H:i:s', time())
            ];
            $ActivityModel = new Activity();
            $info = $ActivityModel->imgsadd($imgsadd);
        }
        if (!empty($info)) {
            return response()->json(['code' => 0, 'msg' => '添加成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }

    //图片删除
    public function imgsdel($id)
    {
        $del = [
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        $ActivityModel = new Activity();
        if ($ActivityModel->imgsdel($del, $id)) {
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }

//报名管理
    public function enroll($id)
    {

        $this->_response['_active']['_action'] = 'enroll';

        //获取谁报的名
        $enrollModel = new Activity();
        $enroll = $enrollModel->enroll(['activity_id' => $id]);
        $this->_response['id'] = $id;
        //获取报名的联系人
        $enrollIds = [];
        foreach ($enroll['list'] as $e){
            $enrollIds[] = $e->eid;
        }
        $personal = $enrollModel->getEnrollPersonal($enrollIds);
        //组装数据

        $userPersonal = [];

       foreach ($enroll['list'] as $k => $e) {
           $e->personal = [];
           foreach ($personal as $p) {
               if ($e->eid == $p->enroll_id) {
                   $e->personal[] = $p;
               }
           }

           if (empty($e->personal)) {
               unset($enroll['list'][$k]);
           } else {
               if (empty($userPersonal[$e->uid])) {
                   $userPersonal[$e->uid] = $e->personal;
               } else {
                   $userPersonal[$e->uid] = array_merge($userPersonal[$e->uid], $e->personal);
                   unset($enroll['list'][$k]);
               }
           }
       }
           /* foreach ($enroll['list'] as $e){
                       foreach ($userPersonal as $key => $value){
                           if($key === $e->uid){
                               $e->personal = $value;
                           }
                       }
                   }*/

        $this->_response['enroll'] = $enroll;
        // return  $this->_response;
          return view('admin.activity.enroll', $this->_response);
    }
//历史报名
    public function HistoryEnroll($id)
    {
        $this->_response['_active']['_action'] = 'history';

        //获取谁报的名
        $enrollModel = new Activity();
        $enroll = $enrollModel->enroll(['activity_id' => $id]);
        $this->_response['id'] = $id;
        //获取报名的联系人
        $enrollIds = [];
        foreach ($enroll['list'] as $e){
            $enrollIds[] = $e->eid;
        }
        $personal = $enrollModel->HistoryEnroll($enrollIds);
        //组装数据

        $userPersonal = [];

        foreach ($enroll['list'] as $k => $e) {
            $e->personal = [];
            foreach ($personal as $p) {
                if ($e->eid == $p->enroll_id) {
                    $e->personal[] = $p;
                }
            }

            if (empty($e->personal)) {
                unset($enroll['list'][$k]);
            } else {
                if (empty($userPersonal[$e->uid])) {
                    $userPersonal[$e->uid] = $e->personal;
                } else {
                    $userPersonal[$e->uid] = array_merge($userPersonal[$e->uid], $e->personal);
                    unset($enroll['list'][$k]);
                }
            }
        }

        $this->_response['enroll'] = $enroll;
        // return  $this->_response;
         return view('admin.activity.enrollHistory', $this->_response);
    }

    //报名修改积分
    public function enrolledit($id){
        $this->_response['_active']['_action']      = 'enrolledit';
        $userModel = new User();
        $this->_response['enrolledit'] = $userModel::where('id', $id)->first();
        //return $this->_response;
        return view('admin.activity.enrolledit', $this->_response);
    }
    public function enrolldoEdit(\App\Http\Requests\Enroll $request){
        $data = $request->all();
        $integralEdit = [
            'id' => $data['id'],
            'integral' => $data['integral'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];
        $ActivityModel = new Activity();
        if($ActivityModel->integralEdit($integralEdit,$data['id'])){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }



    //报名删除页面
    public function enrolldelete($enrollId,$personalId){
        $ActivityModel = new Activity();
        $Del = [
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        if ($ActivityModel->personalupdate($enrollId,$personalId, $Del)) {
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }
    //报名修改备注
    public function enreason($enrollId,$personalId){
        $this->_response['_active']['_action']      = 'enreason';
        $enroll_personalModel = new EnrollPersonal();
        $this->_response['enreason'] = $enroll_personalModel::where('enroll_id',$enrollId)->where('personal_id',$personalId)->first();
        // return  $this->_response['enreason'];
        return view('admin.activity.enreason', $this->_response);
    }
    public function doEnReason(\App\Http\Requests\Enroll $request){
        $data = $request->all();
        $reason = [
            'reason' => $data['reason'],
        ];
        $ActivityModel = new Activity();
        if($ActivityModel->personalupdate($data['enroll_id'],$data['personal_id'],$reason)){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }
}